import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../r.dart';

class GCDialog extends StatefulWidget {
  // 内容区域布局
  TextAlign contentAlign;

  Widget title;

  String content;

  // 点击返回index 0 1
  Function onTap;

  //按钮
  List<String> items;

  GCDialog({
    this.contentAlign = TextAlign.left,
    this.onTap,
    this.items,
    this.content,
    this.title,
  });

  @override
  _GCDialogState createState() => _GCDialogState();
}

class _GCDialogState extends State<GCDialog> {
  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Center(
        // ClipRRect 创建圆角矩形 要不然发现下边button不是圆角
        child: ClipRRect(
          borderRadius: BorderRadius.circular(10.0),
          child: Container(
            color: Colors.white,
            width: 250,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                SizedBox(height: 20),
                showX(),
                showTitle(),
                SizedBox(height: 10),
                Container(
                  margin: EdgeInsets.only(left: 15, right: 15),
                  child: Text(
                    widget.content,
                    style: TextStyle(
                      color: Color(0xFF666666),
                      fontSize: 14,
                    ),
                  ),
                ),
                SizedBox(height: 20),
                showButton(),
//                _itemCreat(),
                SizedBox(height: 20),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _itemCreat() {
    return Container(
      child: Row(
        children: widget.items.map((res) {
          int index = widget.items.indexOf(res);
          return Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () {
                Navigator.pop(context);
                widget.onTap(index);
              },
              child: Container(
                height: 27,
                alignment: Alignment.center,
                child: Text(
                  res,
                  style: TextStyle(color: Colors.black, fontSize: 15),
                ),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }

  showTitle() {
    if (widget.title != null) {
      return widget.title;
    } else {
      return Container();
    }
  }

  showButton() {
    if (widget.items != null) {
      return Container(
        height: 27,
        child: Row(
          children: [
            SizedBox(width: 27),
            Container(
                width: 90,
                child: RaisedButton(
                  child: Text("${widget.items[0]}",
                      style: TextStyle(fontSize: 14, color: Color(0xFF666666))),
                  color: Color(0xFFF4F4F4),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                )),
            SizedBox(width: 20),
            Container(
                width: 90,
                child: RaisedButton(
                  child: Text("${widget.items[1]}",
                      style: TextStyle(fontSize: 14, color: Colors.white)),
                  color: Theme.of(Get.context).primaryColor,
                  onPressed: () {
                    widget.onTap(0);
                    Navigator.pop(context);
                  },
                )),
          ],
        ),
      );
    } else {
      return Container();
    }
  }

  showX() {
    if (widget.items != null) {
      return Container();
    } else {
      return Row(
        children: [
          Expanded(child: SizedBox()),
          GestureDetector(
              child: Icon(Icons.clear),
              onTap: () {
                Navigator.pop(context);
              }),
          SizedBox(width: 20)
        ],
      );
    }
  }
}
